<?php

namespace App\Admin\Controllers;


use App\Admin\Actions\Grid\RowActions\AuditRemarkAction;
use App\Admin\Actions\Grid\RowActions\ToPayAction;
use App\Admin\Actions\Grid\RowActions\UploadReceiptFormAction;
use App\Admin\Actions\Grid\Tools\ToPayBatchTools;
use App\Admin\Actions\Grid\Tools\UploadReceiptFormBatchTools;
use App\Admin\Repositories\ProjectAgreement;
use App\Admin\Repositories\ProjectExportRecordsDetail;
use App\Support\Data;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

/**
 * 导出记录详情
 *
 * Class ProjectExportRecordsDetailController
 * @package App\Admin\Controllers
 */
class ProjectExportRecordsDetailController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProjectExportRecordsDetail(['project', 'user']), function (Grid $grid) {

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disablePerPages();
            $grid->paginate(10);

            // 禁用复选框、批量删除
            // $grid->disableRowSelector();
            $grid->disableBatchDelete();

            $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);

            $records_id = request()->input('records_id', 0);

            $grid->model()->where(
                'records_id',
                $records_id
            );
            // 判断是否有批量支付与批量上传回执单权限
            if (Admin::user()->can('batch-to-pay') && Admin::user()->can('batch-receipt-form')) {
                $grid->tools([
                    new ToPayBatchTools('批量支付劳务'),
                    // new UploadReceiptFormBatchTools(),
                ]);
            }

            $grid->column('id');
            $grid->column('project.project_name', '项目名称');
            $grid->column('project.project_sn', '项目编号');
            $grid->column('agreement_code', '协议编号');
            $grid->column('bank_card', '银行卡号');
            $grid->column('user_auth_name', '开户名');
            $grid->column('bank_name', '开户银行');
            $grid->column('bank_addr', '开户地');
            $grid->column('user.id_number', '身份证号');
            $grid->column('user.phone', '收款人手机号码');

            $grid->column('pay_status', '劳务支付状态')->display(function ($payStatus) {
                $status = ProjectAgreement::getPayStatus();
                return $status[$payStatus];
            })->label(['1' => 'info', 2 => 'success', '-1' => 'danger']);;

            $grid->column('paymentd_at', '劳务支付时间');

            // $grid->column('税前金额')->display(function () {
            //     list($tax, $beforeTaxAmount) = Data::getTaxRate($this->fee_type_value);
            //     return $beforeTaxAmount;
            // });
            // $grid->column('纳税金额')->display(function () {
            //     list($tax, $beforeTaxAmount) = Data::getTaxRate($this->fee_type_value);
            //     return $tax;
            // });

            $grid->column('fee_type_value', '税后金额')->display(function () {
                return number_format($this->fee_type_value, 2, '.', '');
            });



            // $grid->column('audit_first', '协议审核状态')
            //     ->using(ProjectAgreement::getAuditStatus('audit_first'))
            //     ->label([1 => 'success', '-1' => 'danger']);
            // $grid->column('audit_fee', '劳务审核状态')
            //     ->using(ProjectAgreement::getAuditStatus('audit_fee'))
            //     ->label([1 => 'success', '-1' => 'danger']);

            $grid->column('receipt_form', '回执单')->display(function ($pictures) {
                return explode(',', $pictures);
            })->image('', 100, 100);
            // $grid->column('receipt_form', '回执单')->display(function ($url) {
            //     return '<a href="' . $url . '" target="_blank"><i class="fa fa-download"></i> 回执单</a>';
            // });

            $grid->column('created_at')->sortable();

            $grid->model()->orderBy('created_at', 'desc')->orderBy('id', 'desc');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (Admin::user()->can('check-agreement-audit-view')) {
                    if ($this->audit_first == -1 || $this->audit_fee == -1) {
                        $actions->append(new AuditRemarkAction()); // 审核意见
                    }
                }
                // 判断是否有支付与上传回执单权限
                if (Admin::user()->can('to-pay')) {
                    // if ($this->pay_status == 1) {
                    $actions->append(new ToPayAction('去支付'));
                    // }
                }

                // if (Admin::user()->can('upload-receipt-form')) {
                //     $actions->append(new UploadReceiptFormAction('上传回执单'));
                // }
            });


            $grid->tools('<a href="javascript:history.back(-1)" class="btn btn-primary dialog-create">返回</a>');

            $grid->filter(function (Grid\Filter $filter) {
                // $filter->equal('project_sn', '项目编号');
                $filter->equal('agreement_code');
                $filter->equal('bank_card', '银行卡号');
                // $filter->like('active_name', '活动名称');
                $filter->between('active_date', '会议日期')->date();
                $filter->between('active_time', '会议时间')->time();
                $filter->equal('project_id', '所属项目')
                    ->select(\App\Models\Project::select('id', 'project_name')->pluck('project_name', 'id'));
                $filter->equal('pay_status', '支付状态')->select(ProjectAgreement::getPayStatus());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProjectExportRecordsDetail(), function (Show $show) {
            $show->field('id');
            $show->field('project_id');
            $show->field('user_id');
            $show->field('agreement_code');
            $show->field('user_auth_name');
            $show->field('bank_card');
            $show->field('bank_username');
            $show->field('bank_name');
            $show->field('bank_addr');
            $show->field('id_card');
            $show->field('mobile');
            $show->field('company');
            $show->field('fee_type_key');
            $show->field('fee_type_value');
            $show->field('role');
            $show->field('active_date');
            $show->field('active_week');
            $show->field('active_time_begin');
            $show->field('active_time_end');
            $show->field('express_name');
            $show->field('express_code');
            $show->field('status');
            $show->field('pay_status');
            $show->field('audit_global_status');
            $show->field('audit_remark');
            $show->field('audit_fee');
            $show->field('audit_first');
            $show->field('audit_second');
            $show->field('audit_final');
            $show->field('audit_log');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProjectExportRecordsDetail(), function (Form $form) {
            $form->display('id');
            $form->text('project_id');
            $form->text('user_id');
            $form->text('agreement_code');
            $form->text('user_auth_name');
            $form->text('bank_card');
            $form->text('bank_username');
            $form->text('bank_name');
            $form->text('bank_addr');
            $form->text('id_card');
            $form->text('mobile');
            $form->text('company');
            $form->text('fee_type_key');
            $form->text('fee_type_value');
            $form->text('role');
            $form->text('active_date');
            $form->text('active_week');
            $form->text('active_time_begin');
            $form->text('active_time_end');
            $form->text('express_name');
            $form->text('express_code');
            $form->text('status');
            $form->text('pay_status');
            $form->text('audit_global_status');
            $form->text('audit_remark');
            $form->text('audit_fee');
            $form->text('audit_first');
            $form->text('audit_second');
            $form->text('audit_final');
            $form->text('audit_log');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
